<?php
/**
 * Description of JobAction
 *
 * @author chacha
 * @date 2015-12-19
 */
class JobAction extends BaseAction{
    
    public function _empty() {
        $tpl = "../Include/App/Group/Bridge/Tpl/Public_404.html";
        $this->display($tpl);
    }
    
    public function index() {
        $id = I('job', 1);
        if ($id==1) {
            $this->redirect('/Index/projects@'.getDomain());
        }
        $prjTable = array('xia_project' => 'arr', 'xia_team' => 'team', 'xia_user_info'=>'uif');
        $prjField = 'arr.*,team.name as tname,team.logo,team.weibo,team.weixin,uif.nickname as creater';
        $prjWhere = 'arr.id=' . $id . ' and arr.tid=team.id and team.uid=uif.uid';
        $prjInfo = M('project')->table($prjTable)->field($prjField)->where($prjWhere)->find();
        $julTable = array('xia_job_user' => 'list', 'xia_job' => 'job', 'xia_user_info' => 'user');
        $julField = 'list.*,job.name as jname,user.nickname,user.photo';
        $julWhere = 'list.pid=' . $id . ' and list.jobid=job.id and job.state>0 and list.uid=user.uid and (list.state=2 or list.state=4 or list.state=5)';
        $jobUsers = M("job_user")->table($julTable)->field($julField)->where($julWhere)->select();
        $jlField = "j.*,(select COUNT(*) from xia_jobfav jf where j.id=jf.jobid) as fc,(select COUNT(*) from xia_job_user ju where j.id=ju.jobid and (ju.state=0 or ju.state=2)) as aplc";
        $order = "(case j.moneyin when 1 then 4 when 4 then 3 when 2 then 2 when 0 then 1 end) desc, (j.num-j.datastate) desc, j.ctime";
        $jobs = M('job')->table(array('xia_job'=>'j'))->field($jlField)->where('j.pid=' . $id. ' and j.state>0')->order($order)->select();//职位统计
        //$jmFiled = "j.id,(select COUNT(*) from xia_jobfav if where j.id=jf.jobid and jf.uid=".$this->uid.")";
        $resMfav = M()->table(array('xia_jobfav'=>'jf'))->where('jf.uid=' . $this->uid)->select();//当前用户收藏的
        $jobTotal = M('job')->where('pid=' . $id.' and state>0')->sum('num');
        if (!$jobTotal) {
            $jobTotal = 0;
        }
        $rest = $jobTotal - count($jobUsers);
        $rest = ($rest >= 0) ? $rest : 0;
        
        $alljobs = array();
        //已申请
        $applieds = M('job_user')->where('uid='.$this->uid.' and state=0')->select();
        foreach ($jobs as $job) {
            $job['applied'] = 0;
            foreach ($applieds as $applied) {
                if ($job['id']==$applied['jobid']) {
                    $job['applied'] = 1;
                }
            }
            $job['faved'] = 0;
            foreach ($resMfav as $fav) {
                if ($job['id']==$fav['jobid']) {
                    $job['faved'] = 1;
                }
            }
            $alljobs[] = $job;
        }
        
//        echo $this->uid;
//        exit;
        
        //屏蔽创建者email
        $prjInfo['creater'] = convEmail($prjInfo['creater']);
        
        $this->assign("prjInfo", $prjInfo);
        $this->assign("jobUsers", $jobUsers);
        $this->assign("pid", $id);
        $this->assign("uid", $this->uid);
        $this->assign("jobTotal", $jobTotal);
        $this->assign('countData', count($jobUsers));
        $this->assign("rest", $rest);
        $this->assign("jobs", $alljobs);
        $this->assign('tip', C('PRJ_MONEY_TUOGUAN_TIP_BROWSER'));
        $this->assign("on", 2);
        $this->display("index");
    }
}
